/**
 * AluiCommons -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of AluiCommons.
 * 
 * AluiCommons is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AluiCommons is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AluiCommons.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Data;
using System.Configuration;
using com.plumtree.server;
using com.plumtree.server.impl.community;
using com.plumtree.openfoundation.util;
using com.plumtree.openlog;
using AluiServerLib.Business;
using System.Collections;

namespace AluiServerLib.Models
{
    public class PTUserModel
    {
        private static OpenLogger log = OpenLogService.GetLogger(
            "AluiServerLib",
            "AluiServerLib.Models.PTUserModel");

        private IPTSession m_ptSession = null;
        private IPTUserManager m_ptUserManager = null;

        public PTUserModel(IPTSession ptSession)
        {
            m_ptSession = ptSession;
            m_ptUserManager = (IPTUserManager)ptSession.GetUsers();
        }

        public void Release()
        {
            m_ptSession = null;
            m_ptUserManager = null;
        }

        public IPTSession PTSession
        {
            get
            {
                return m_ptSession;
            }
        }

        public ArrayList GetUsers(int adminfolderid, bool includesubfolders)
        {
            ArrayList arrPages = new ArrayList();

            //create search request
            IPTSearchRequest req = m_ptSession.GetSearchRequest();

            if (adminfolderid > 0)
            {
                req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_ADMINFOLDERS, new int[] { adminfolderid });
            }

            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_INCLUDE_SUBFOLDERS, includesubfolders);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_OBJTYPES, new int[] { PT_CLASSIDS.PT_PAGE_ID });

            int[] arPropIDs = { PT_INTRINSICS.PT_PROPERTY_OBJECTID, PT_INTRINSICS.PT_PROPERTY_OBJECTNAME, PT_INTRINSICS.PT_PROPERTY_OBJECTSUMMARY };
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_RET_PROPS, arPropIDs);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_INCLUDE_USUAL_FIELDS, false);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_KWIC, false);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_BESTBETS, false);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_SPELLCHECK, false);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_SKIPRESULTS, 0);
            req.SetSettings(PT_SEARCH_SETTING.PT_SEARCHSETTING_MAXRESULTS, 10000);

            //create filter
            IPTFilter filter = PortalObjectsFactory.CreateSearchFilter();
            IPTPropertyFilterStatement statement = (IPTPropertyFilterStatement)filter.GetNewFilterItem(PT_FILTER_ITEM_TYPES.PT_FILTER_ITEM_STATEMENT);
            statement.SetOperand(PT_INTRINSICS.PT_PROPERTY_OBJECTID);
            statement.SetOperator(PT_FILTEROPS.PT_FILTEROP_GT);
            statement.SetValue(0);
            filter.SetPropertyFilter(statement);

            IPTSearchQuery query = req.CreateAdvancedQuery(filter);

            IPTSearchResponse ptPagesResponse = req.Search(query);
            int nResultCount = ptPagesResponse.GetResultsReturned();

            for (int nIndex = 0; nIndex < nResultCount; nIndex++)
            {
                ALUIPage page = new ALUIPage();
                page.SetObjectID(ptPagesResponse.GetFieldsAsInt(nIndex, PT_INTRINSICS.PT_PROPERTY_OBJECTID));
                page.SetName(ptPagesResponse.GetFieldsAsString(nIndex, PT_INTRINSICS.PT_PROPERTY_OBJECTNAME));
                page.SetDescription(ptPagesResponse.GetFieldsAsString(nIndex, PT_INTRINSICS.PT_PROPERTY_OBJECTSUMMARY));

                arrPages.Add(page);
            }
            return arrPages;
        }

    }

}
